<template>
	<view>
		<!-- 搜索栏 -->
		<view class="search-container">
			<input type="text" v-model="searchValue" placeholder="请输入搜索内容" class="search-input" />
		</view>

		<!-- 轮播图 -->
		<view class="swiper-container">
			<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="500" :circular="true">
				<swiper-item v-for="(item, index) in lunbo" :key="index">
					<image :src="item.img" mode="aspectFill" class="swiper-image"></image>
				</swiper-item>
			</swiper>
		</view>

		<!-- 菜单栏 -->
		<view class="menu-container">
			<view class="menu-item" v-for="(item, index) in menuList" :key="index">
				<image :src="item.icon" mode="aspectFit" class="menu-icon"></image>
				<text class="menu-title">{{ item.title }}</text>
			</view>
		</view>

		<!-- 商品列表 -->
		<view class="product-list">
			<view class="product-item" v-for="(item,index) in proList" :key="index" @click="goDeteails(item.proid)">
				<image :src="item.img1" mode="aspectFill" class="product-image"></image>
				<view class="product-info" @click="goDeteails">
					<text class="product-name">{{ item.proname }}</text>
					<text class="product-price">¥{{ item.originprice }}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue';
	//搜索值
	let searchValue = ref('');


	// 轮播图
	let lunbo = ref([]);

	function lunboHttp() {
		uni.request({
			url: 'http://47.94.6.186:3001/api/banner/list',
			method: 'GET'
		}).then(res => {
			// 确保数据正确获取
			if (res.data && res.data.data) {
				lunbo.value = res.data.data;
			}
		})
	}

	// 页面加载时调用
	lunboHttp();

	//菜单栏
	let menuList = ref([]);

	function menuHttp() {
		uni.request({
			url: 'http://47.94.6.186:3001/api/menu/list',
			method: 'GET',
		}).then(res => {
			menuList.value = res.data.data;
			console.log(menuList.value);
		})
	}
	menuHttp();

	//商品列表 - 使用ref使其成为响应式数据
	let proList = ref([]);

	function productHttp() {
		uni.request({
			url: 'http://47.94.6.186:3001/api/pro/list',
			method: 'GET',
			data: {
				count: 1,
				limitNum: 10
			}
		}).then(res => {
			proList.value = res.data.data;

		})
	}
	productHttp();


	//前往详情页方法
	function goDeteails(id) {
		uni.navigateTo({
			url: `/pages/details/details?productid=${id}`
		})
	}
</script>

<style scoped>
	/* 添加搜索栏样式 */
	.search-container {
		padding: 20rpx;
		background-color: #f8f8f8;
		/* 确保搜索框容器可见 */
		height: 100rpx;
		display: flex;
		align-items: center;
	}

	/* 原生搜索框样式 */
	.search-input {
		width: 100%;
		height: 60rpx;
		background-color: #fff;
		border-radius: 30rpx;
		padding: 0 20rpx;
		font-size: 28rpx;
	}

	.swiper-container {
		width: 100%;
		overflow: hidden;
	}

	.swiper {
		height: 300rpx;
		/* 设置轮播图高度 */
	}

	.swiper-image {
		width: 100%;
		height: 100%;
	}

	/* 轮播指示器样式 */
	.swiper .wx-swiper-dots {
		bottom: 10rpx;
	}

	.swiper .wx-swiper-dots .wx-swiper-dot {
		width: 12rpx;
		height: 12rpx;
		margin: 0 8rpx;
	}

	/* 商品列表样式 */
	.product-list {
		padding: 10rpx;
	}

	.product-item {
		display: flex;
		padding: 15rpx;
		margin-bottom: 15rpx;
		background-color: #fff;
		border-radius: 10rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
	}

	.product-image {
		width: 180rpx;
		height: 180rpx;
		border-radius: 8rpx;
		margin-right: 20rpx;
	}

	.product-info {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: space-around;
	}

	.product-name {
		font-size: 28rpx;
		color: #333;
		line-height: 40rpx;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		overflow: hidden;
	}

	.product-price {
		font-size: 32rpx;
		color: #e64340;
		font-weight: bold;
	}

	/* 菜单栏样式 */
	.menu-container {
		display: flex;
		flex-wrap: wrap;
		padding: 20rpx;
		background-color: #fff;
	}

	.menu-item {
		width: 20%;
		/* 每行显示5个菜单 */
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-bottom: 20rpx;
	}

	.menu-icon {
		width: 80rpx;
		height: 80rpx;
		border-radius: 50%;
		margin-bottom: 10rpx;
	}

	.menu-title {
		font-size: 24rpx;
		color: #333;
		text-align: center;
	}
</style>